package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sina.weibo.sdk.api.share.Base;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.activity.data.YueChangeAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.MyYueBianHuaTask;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.R;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/4/15.
 */
public class ZhangHuYueActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener, View.OnClickListener {
    private Context mContext;
    private TextView back;
    private TextView mZhangHuYue;
    //    private PullToRefreshListView mListView;
//    private YueChangeAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private String gameid;
    private int mYueChangeTaskId = -1;
    private TextView shouzhimingxiBtn;
    private Button rechargeBtn;
    //    private TextView yijihuoText;
//    private TextView daijihuoText;
    private Button tixianBtn;
    private EatShowUserInfo mUserInfo;
    private int shenfen;
    private String yue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhanghuyue);
        mContext = this;
        initView();
    }

    private void initView() {
        mUserInfo = getEatShowApp().getmUserInfo();
        if(mUserInfo!=null){
            shenfen = mUserInfo.getUserType();
        }
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mZhangHuYue = (TextView) findViewById(R.id.zhanghuyue);
        shouzhimingxiBtn = (TextView) findViewById(R.id.shouzhimingxi);
        shouzhimingxiBtn.setOnClickListener(this);
        rechargeBtn = (Button) findViewById(R.id.recharge_btn);
        rechargeBtn.setOnClickListener(this);
        tixianBtn = (Button) findViewById(R.id.tixian_btn);
        tixianBtn.setOnClickListener(this);
//        if(shenfen == 1){
//            tixianBtn.setVisibility(View.VISIBLE);
//        }else {
//            tixianBtn.setVisibility(View.GONE);
//        }
//        yijihuoText = (TextView) findViewById(R.id.flour_yijihuo);
//        daijihuoText = (TextView) findViewById(R.id.flour_daijihuo);

        getYueChangeList();
//        mListView = (PullToRefreshListView)findViewById(R.id.yue_list);
//        mListView.init(mContext,true);
//        mAdapter = new YueChangeAdapter(mContext);
//        mListView.setAdapter(mAdapter, true);
//        mListView.setOnRefreshListener(this);
//        mListView.setOnLoadDataListener(this);
//        mListView.doRefresh();
    }

    private void getYueChangeList() {
        if (mYueChangeTaskId != -1) {
            return;
        }
        if (currentPage == 0) {
//            mListView.setNoNextPage();
            return;
        }

        MyYueBianHuaTask stask = new MyYueBianHuaTask(size, currentPage);
        stask.addListener(changelistener);
        mYueChangeTaskId = getEatShowApp().getTaskManager().addTask(stask);

    }

    @Override
    protected void onResume() {
        super.onResume();
        getYueChangeList();
    }

    private TaskEventListener changelistener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == mYueChangeTaskId) {
                mYueChangeTaskId = -1;
//                if(currentPage == 1){
//                    mAdapter.clearData();
//                    mListView.onRefreshComplete();
//                    mListView.enableLoadMore();
//                }else{
//                    mListView.onLoadDataComplete(true);
//                }
                List<OrderInfo> mfoodlist = new ArrayList<OrderInfo>();
                if (data != null) {
                    int pageCount = 0;
                    int currentPage = 0;
                    int nextPage = 0;

                    JSONObject jsonString = (JSONObject) data;


//                        currentPage = jsonString.getIntValue(ProtocolConst.CURRENT_PAGE);
//                        pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
//                        nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
                    yue = jsonString.getString("nowBalance");
                    String locked = jsonString.getString("lockFlowerCount");
                    String unlocked = jsonString.getString("unlockFlowerCount");
                    mZhangHuYue.setText("￥" + yue);
//                        yijihuoText.setText(unlocked+"朵");
//                        daijihuoText.setText(locked+"朵");
//                        mfoodlist = JSON.parseArray(jsonString.getString(ProtocolConst.LIST_TAG), OrderInfo.class);

                }

//                if(mfoodlist != null && mfoodlist.size()>0){
//                    mAdapter.updateData(mfoodlist);
//                }

//                currentPage = result;


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == mYueChangeTaskId) {
                mYueChangeTaskId = -1;
//                if(currentPage == 1){
//                    mListView.onRefreshComplete();
//                }else{
//                    mListView.onLoadDataComplete(false);
//                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getYueChangeList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getYueChangeList();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shouzhimingxi:
                Intent s_intent = new Intent(mContext, ShouzhimingxiActivity.class);
                startActivity(s_intent);
                break;
            case R.id.recharge_btn:
                Intent intent = new Intent(mContext, RechargeActivity.class);
                startActivity(intent);
                break;
            case R.id.tixian_btn:
                Intent tIntent = new Intent(mContext,TiXianActivity.class);
                tIntent.putExtra("FROM",0);
                tIntent.putExtra("YUE",yue);
                startActivity(tIntent);
                break;
        }
    }
}
